/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MatrixCalculation;

import RatingMatrix.RandomNodes;
import java.text.DecimalFormat;

/**
 *
 * @author Ekantik
 */
class NormalizedMatrix {
    RatingMatrix rm;
    double normalizedMatrix[][];
    SimilarityMatrix sm;
    double summation[];
    double dOTrust[][];

    public double[][] getdOTrust() {
        return dOTrust;
    }

    public NormalizedMatrix() {
        this.rm = new RatingMatrix();
        //System.out.println("hi i m here");
        SimilarityMatrix sm = new SimilarityMatrix(rm);
        this.sm = sm;
        this.prepareNormalizedMatrix();
        this.degreeOfTrust();
        //System.out.println("Rating Matrix:-");
        //this.print(rm.getRatMatrix());
        //System.out.println("Normalized Matrix is :- ");
        //this.print(this.getNormalizedMatrix());
        //System.out.println("Degree of Trust Matrix is :-");
        //this.print(this.getdOTrust());
    }

    
    public void setdOTrust(double[][] dOTrust) {
        this.dOTrust = dOTrust;
    }


    public double[] getSummation() {
        return summation;
    }

    public void setSummation(double[] summation) {
        this.summation = summation;
    }
    
/*    public void NormalizedMatrix(){
                System.out.println("hi i m here");
        /*this.rm = new RatingMatrix();
        System.out.println("hi i m here");
        SimilarityMatrix sm = new SimilarityMatrix(rm);
        this.sm = sm;
        this.prepareNormalizedMatrix();
        this.degreeOfTrust();
        this.print(this.getdOTrust());
    }*/
    
    public void prepareNormalizedMatrix(){
        double ratMean[][] = new double[rm.getNoOfUsers()][rm.getNoOfServices()];
        sm.prepareMean(rm);
        sm.calculateSim();
        sm.scaledSimilarityMatrix();
        //double d = -0.26+1.0-0.05+0.29	-0.07	-0.72	-0.4	+0.03	-0.29	+0.49	;
        //System.out.println(d);
        for(int i = 0;i<rm.getNoOfUsers();i++){
            for(int j = 0;j<rm.getNoOfServices();j++){
                ratMean[i][j] = rm.getRatMatrix()[i][j] - sm.getMean()[i];
            }
                    
        }
        MatrixMultiplication mm = new MatrixMultiplication(sm.getSimilarityMatrix(), ratMean, rm.getNoOfUsers(), rm.getNoOfServices());
        //this.print(sm.getSimilarityMatrix());
        normalizedMatrix = new double[rm.getNoOfUsers()][rm.getNoOfServices()];
        this.prepareSummation();
        double numerator = 0;
        double denominator = 0;
        for(int u = 0;u<rm.getNoOfUsers();u++){
            for(int v = 0;v<rm.getNoOfServices();v++){
                numerator = this.RoundTo2Decimals(mm.getDoubleResult()[u][v]);
                denominator = this.RoundTo2Decimals(this.getSummation()[u]);
                normalizedMatrix[u][v] = this.RoundTo2Decimals(sm.getMean()[u] +(numerator/denominator));
                //System.out.print(mm.getDoubleResult()[u][v]+"/"+this.getSummation()[u] +"\t");
                //System.out.println(normalizedMatrix[u][v]);
            }
            //System.out.println();
        }
        //this.print(this.getNormalizedMatrix());
    }
    public double[] prepareSummation(){
        this.summation = new double[rm.getNoOfUsers()];
        for(int i = 0;i<rm.getNoOfUsers();i++){
            for(int j = 0;j<rm.getNoOfUsers();j++){
                this.summation[i] +=Math.abs(this.RoundTo2Decimals(sm.getSimilarityMatrix()[i][j]));
            }
                    //System.out.println(summation[i]);
        }
        return summation;
    }
    
    double RoundTo2Decimals(double val) {
            DecimalFormat df2 = new DecimalFormat("###.##");
        return Double.valueOf(df2.format(val));
    }
    
    public static void main(String args[]){

        NormalizedMatrix nm = new NormalizedMatrix();

    }
    /**
     *
     * @param matrix
     */
    public void print(int matrix[][]){
        for(int i=0;i<rm.getNoOfUsers();i++){
            for(int j=0;j<rm.getNoOfServices();j++){
                //System.out.print("["+i+"]["+j+"]"+matrix[i][j]+"\t");
                System.out.print(matrix[i][j]+"\t");
            }
            System.out.println();
        }
    }
    public void print(double matrix[][]){
        for(int i=0;i<rm.getNoOfUsers();i++){
            for(int j=0;j<rm.getNoOfServices();j++){
                //System.out.print("["+i+"]["+j+"]"+matrix[i][j]+"\t");
                System.out.print(this.RoundTo2Decimals(matrix[i][j])+"\t");
            }
            System.out.println();
        }
    }
    public void degreeOfTrust(){
        dOTrust = new double[rm.getNoOfUsers()][rm.getNoOfServices()];
        double sum;
        for(int u = 0;u<rm.getNoOfUsers();u++){
            for(int i = 0;i<rm.getNoOfServices();i++){
                dOTrust[u][i] = 1 - ((Math.abs(this.getNormalizedMatrix()[u][i] - rm.getRatMatrix()[u][i]))/5); //changes are to be made here.
            }
        }
        this.setdOTrust(dOTrust);
    }

    public double[][] getNormalizedMatrix() {
        return normalizedMatrix;
    }

    public void setNormalizedMatrix(double[][] normalizedMatrix) {
        this.normalizedMatrix = normalizedMatrix;
    }
}
